import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    showNum: '0', // 展示的数字
    lastNum: '', // 上一个展示的数字
    results: '',
    formula: '', // 展示表达式
    operateCode: '', // 储存操作符
    numList: [], // 保存两个操作数
    isOperate: false, // 判断是否按了加减乘除
    isReinput: false, // 判断是否按了等于键
    isFirstEqual: 0, // 计算按等于键的次数
    isFirstOperate: 0, // 计算按加减乘除的次数
    isMath: false, // 判断是否进行了Math运算
    isMathHe: false // 判断是否进行了Math运算, 用于输入数字
  },
  mutations: {
    heBing (state, num) {
      state.isOperate = false
      if (state.showNum === '除数不能为零') state.showNum = ''
      if (state.isReinput && !state.isMathHe) {
        state.isReinput = false
        this.commit('clearAll')
      }
      if (!state.showNum.includes('.') && state.showNum[0] === '0') state.showNum = state.showNum.slice(1)
      state.showNum += num
      if (state.showNum === '.') state.showNum = '0.'
    },

    clearShowNum (state) {
      if (state.formula === '') {
        state.showNum = '0'
        state.lastNum = ''
      } else if (state.formula.includes('=')) {
        this.commit('clearAll')
      } else if (!state.formula.includes('=')) {
        state.showNum = '0'
      }
    },

    clearAll (state) {
      state.showNum = '0'
      state.lastNum = ''
      state.formula = ''
      state.operateCode = ''
      state.numList = []
      state.isFirstOperate = 0
      state.isFirstEqual = 0
    },

    changePositive (state) {
      let num = Number(state.showNum)
      let num2 = Number(state.lastNum)
      num = -num
      num2 = -num2
      state.showNum = num.toString()
      state.lastNum = num2.toString()
    },

    delLast (state) {
      // 删除当前显示的最后一个字符
      if (state.showNum.length > 0) state.showNum = state.showNum.slice(0, -1)
    },

    toCalculate (state, operate) {
      if (state.showNum === '') state.showNum = '0'
      if (operate !== '=') {
        if (state.isFirstOperate === 0) {
          state.lastNum = state.showNum
          state.numList[0] = state.lastNum
          state.isOperate = true
          state.isFirstOperate++
        }
        if (state.isFirstOperate !== 0) {
          state.numList[0] = state.showNum
        }
        state.operateCode = operate
        state.showNum = ''
        state.formula = state.numList[0] + operate
      } else {
        state.numList[1] = state.showNum
        if (state.numList[0]) this.commit('toResults', state.operateCode)

        if (state.isFirstEqual === 0 && !state.isMath) {
          state.formula += (state.numList[1] + '=')
          state.isFirstEqual++
          state.isMath = false
        } else {
          if (state.showNum === '除数不能为零') state.showNum = '0'
          if (!state.numList[0]) return (state.formula = state.numList[1] + '=')
          state.formula = state.numList[0] + state.operateCode + state.numList[1] + '='
        }
      }
    },

    toResults (state, operate) {
      state.isReinput = true
      if (operate === '/') {
        if (state.numList[1] === '0') {
          state.showNum = '除数不能为零'
          return
        }
        state.showNum = Number(state.numList[0]) / Number(state.numList[1])
      } else if (operate === 'x') {
        state.showNum = Number(state.numList[0]) * Number(state.numList[1])
      } else if (operate === '-') {
        state.showNum = Number(state.numList[0]) - Number(state.numList[1])
      } else {
        state.showNum = Number(state.numList[0]) + Number(state.numList[1])
      }
      state.showNum = state.showNum.toString()
    },

    toMath (state, operate) {
      state.formula = ''
      state.isMath = true
      state.isMathHe = true
      state.isReinput = true
      if (operate === '1/x') {
        if (state.showNum === '0' || state.showNum === '') {
          state.showNum = '除数不能为零'
          return
        }
        state.formula = '1/' + state.showNum + '='
        state.showNum = (1 / Number(state.showNum)).toString()
      } else if (operate === '**') {
        state.formula = state.showNum + '*' + state.showNum + '='
        state.showNum = Number(state.showNum) * Number(state.showNum) + ''
      } else if (operate === '//') {
        if (state.showNum.includes('-')) {
          state.showNum = '不能为负数'
          return
        }
        state.showNum = (Math.sqrt(Number(state.showNum))).toString()
      }
    }
  }
})
